package org.qhhc.compare;

import java.util.Arrays;

public class ComparableTest {

    // 自定义对象实现 Comparable 接口（自然排序规则）
    static class Employee implements Comparable<Employee> {
        private String name;
        private double salary;

        public Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }

        @Override
        public int compareTo(Employee other) {
            // 按薪资升序排序（若需降序可交换 other 和 this）
            return Double.compare(this.salary, other.salary);
        }

        @Override
        public String toString() {
            return "Employee{name='" + name + "', salary=" + salary + "}";
        }
    }

    public static void main(String[] args) {
        // 创建对象数组
        Employee[] employees = {
                new Employee("Alice", 85000),
                new Employee("Bob", 75000),
                new Employee("Charlie", 92000)
        };

        // 直接调用 Arrays.sort 进行排序（无需额外比较器）
        Arrays.sort(employees);

        // 输出排序结果
        System.out.println("按薪资升序排序：");
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }
}